package com.lj.demo.cmsservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lj.demo.cmsservice.entity.CrmBanner;
import com.lj.demo.cmsservice.mapper.CrmBannerMapper;
import com.lj.demo.cmsservice.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;



import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author liujiang
 * @since 2020-12-21
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {

    @Autowired
    private CrmBannerService crmBannerService;

    @Override
    public CrmBanner getBannerById(String id) {
        CrmBanner banner = crmBannerService.getById(id);
        return banner;
    }

    @CacheEvict(value = "banner", allEntries=true)
    @Override
    public void saveBanner(CrmBanner crmBanner) {
        crmBannerService.save(crmBanner);
    }

    @CacheEvict(value = "banner", allEntries=true)
    @Override
    public void updateBannerById(CrmBanner crmBanner) {
        crmBannerService.updateById(crmBanner);
    }

    @CacheEvict(value = "banner", allEntries=true)
    @Override
    public void removeBannerById(String id) {
        crmBannerService.removeById(id);
    }

    @Cacheable(value = "banner", key = "'selectIndexList'")
    @Override
    public List<CrmBanner> selectBannerList() {
        //根据id进行排序排列，显示排列之后的前两条数据
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        //last方法，拼接sql语句
        wrapper.last("limit 4");
        List<CrmBanner> list = baseMapper.selectList(null);
        return list;
    }
}
